package com.yitao.cms.service.user.rollinUser;


import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.user.BuyerUserDto;
import com.yitao.client.model.user.BuyerUserModel;
import com.yitao.client.model.user.QBuyerUserModel;
import com.yitao.cms.utils.BaseUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

/**
 * @Auther: wwl
 * @Date: 2019/4/24 15:18
 * @Description:
 */
@Service
public class BuyerUserService extends GenericEntityService<BuyerUserModel, BuyerUserDto, QBuyerUserModel> {

    public BuyerUserService(EntityManager entityManager) {
        super(entityManager);
    }
    private BaseUtils base = BaseUtils.getInstance();
    /**
     * 插入买手版用户数据!
     * @param buyerUserModel
     * @return
     */
    @Transactional
    public BuyerUserModel insertBuyerUserInfo(BuyerUserModel buyerUserModel){
        if (buyerUserModel!=null && buyerUserModel.getOperationUserId()!=null){
            //判断数据是否存在，存在就修改，不存在就删除
            BuyerUserModel buyerUserModel1 = queryFactory.select(Q).from(Q).where(Q.operationUserId.eq(buyerUserModel.getOperationUserId())).fetchFirst();
            if (buyerUserModel1!=null){
                buyerUserModel.setId(buyerUserModel1.getId());
                buyerUserModel.setBalanceAmount(buyerUserModel1.getBalanceAmount());
                buyerUserModel.setState(buyerUserModel1.getState());
                buyerUserModel.setIsDelete(buyerUserModel1.getIsDelete());
                buyerUserModel.setCreateTime(base.getNow());
            }else{
                buyerUserModel.setBalanceAmount(0L);
                buyerUserModel.setState(true);
                buyerUserModel.setIsDelete(false);
                buyerUserModel.setCreateTime(base.getNow());
            }

            return repository.save(buyerUserModel);
        }
       return null;
    }




}
